package sk.velkavrana.app.ui.components;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import sk.velkavrana.app.service.sharedData.ImportDataListener;
import sk.velkavrana.app.service.sharedData.ProgressEvent;
import sk.velkavrana.app.util.UIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @author mtymes
 * @since May 15, 2009 11:05:30 PM
 */
public class JProgressBarDialog {

    private JFrame frame;
    private JProgressBar filesProgressBar;
    private JLabel filesProgressLabel;
    private JProgressBar rowsProgressBar;
    private JLabel rowsProgressLabel;
    private JLabel statusLabel;


    public JProgressBarDialog(String title) {
        init(title);
    }


    private JPanel initProgressPanel() {
        FormLayout progressLayout = new FormLayout(
                "4dlu, pref:grow, 4dlu, right:pref, 4dlu", // columns
                "2dlu, pref, 2dlu, pref, 4dlu, pref, 2dlu, pref, 4dlu" // rows
        );

        JPanel progressPanel = new JPanel(progressLayout);
        progressPanel.setBorder(BorderFactory.createTitledBorder("Progress"));

        CellConstraints cc = new CellConstraints();

        progressPanel.add(new JLabel("Files progress:"), cc.xy(2, 2));
        filesProgressBar = new JProgressBar();
        filesProgressBar.setMinimum(0);
        filesProgressBar.setMaximum(32);
        filesProgressBar.setValue(10);
        progressPanel.add(filesProgressBar, cc.xy(2, 4));
        filesProgressLabel = new JLabel("0 / 1");
        progressPanel.add(filesProgressLabel, cc.xy(4, 4));

        progressPanel.add(new JLabel("Database row progress:"), cc.xy(2, 6));
        rowsProgressBar = new JProgressBar();
        rowsProgressBar.setMinimum(0);
        rowsProgressBar.setMaximum(387);
        rowsProgressBar.setValue(153);
        progressPanel.add(rowsProgressBar, cc.xy(2, 8));
        rowsProgressLabel = new JLabel("0 / 1");
        progressPanel.add(rowsProgressLabel, cc.xy(4, 8));

        return progressPanel;
    }


    private JPanel initStatusPanel() {
        FormLayout statusLayout = new FormLayout(
                "4dlu, pref:grow, 4dlu", // columns
                "2dlu,  pref, 2dlu" // rows
        );

        JPanel statusPanel = new JPanel(statusLayout);
//        statusPanel.setBorder(BorderFactory.createEtchedBorder());
//        statusPanel.setBorder(BorderFactory.createTitledBorder("Status"));

        CellConstraints cc = new CellConstraints();

        statusLabel = new JLabel("status");
        statusPanel.add(statusLabel, cc.xy(2, 2));

        return statusPanel;
    }


    private void init(JFrame frame) {
        Container content = frame.getContentPane();

        FormLayout formLayout = new FormLayout(
                "2dlu, pref:grow,  2dlu", // columns
                "2dlu, pref, pref, 2dlu" // rows
        );

        content.setLayout(formLayout);

        CellConstraints cc = new CellConstraints();
        content.add(initProgressPanel(), cc.xy(2, 2));
        content.add(initStatusPanel(), cc.xy(2, 3));
    }


    private void init(String title) {

        frame = UIUtil.createFrame(title, 400, 300, false);

        init(frame);

        frame.pack();

        frame.setSize(400, frame.getSize().height);
    }


    private ImportDataListener importDataListener = new ImportDataListener() {

        private void updateProgress(JProgressBar progressBar, JLabel progressLabel, ProgressEvent progressEvent) {
            int value = progressBar.getValue();
            int maximum = progressBar.getMaximum();

            int newValue = progressEvent.getActualPosition();
            int newMaximum = progressEvent.getMaximumCount();

            if (newValue != value) {
                progressBar.setValue(newValue);
            }

            if (newMaximum != maximum) {
                progressBar.setMaximum(newMaximum);
            }

            if (newValue != value || newMaximum != maximum) {
                progressLabel.setText(newValue + " / " + newMaximum);
            }
        }


        public void fileProgressUpdate(ProgressEvent progressEvent) {
            updateProgress(filesProgressBar, filesProgressLabel, progressEvent);
        }


        public void rowProgressUpdate(ProgressEvent progressEvent) {
            updateProgress(rowsProgressBar, rowsProgressLabel, progressEvent);
        }


        public void updateStatus(String newStatus) {
            statusLabel.setText(newStatus);

        }
    };


    public ImportDataListener getImportDataListener() {
        return importDataListener;
    }


    public void setVisible(boolean visible) {
        if (visible) {
            getImportDataListener().fileProgressUpdate(new ProgressEvent(0, 0));
            getImportDataListener().rowProgressUpdate(new ProgressEvent(0, 0));

            statusLabel.setText("");

            UIUtil.center(frame);
            UIUtil.show(frame);
        } else {
            frame.setVisible(false);
        }
    }


}
